#
# Simple makefile to compile the STM32Cube example code given for the STM32F0xx
# family of ARM processors. This file should be placed in the example/demo
# directory that you wish to compile.
#
# INCLUDEDIRS selects all folders in the package that have header files. Since
#             this is already an inclusive list, it should not need editing.
#
# LIBSOURCES contains all the Driver (HAL/BSP) files that need to be compiled
#            for this specific project. This should be updated with the help
#            of the dependencies file (main.d) generated on any (including 
#            unsucessful) compilation.
#
# Note: The provided code sometimes uses windows backslashes (\) in include
#       paths. Update as needed.
#
# Note: When building the lists for LIBSOURCES, *_ex.c files must come before
#       the generic version of the file. This is necessary for linking to find
#       the extended (board-specific) version of the function.
#
# Example: These sources are meant for the CORTEXM_SysTick example. The file
#          should be saved at STM32Cube_FW_F0_V1.1.0/Projects/
#          STM32F072B-Discovery/Examples/Cortex/CORTEXM_SysTick/Makefile.
#
# Author: Ian Hartwig (ihartwig)
#
# Derived from szczys's makefile in 
# www.github.com/szczys/stm32f0-discovery-basic-template/
#

# build environment
CC = arm-none-eabi-gcc-5.4.1
CPP = arm-none-eabi-g++-5.4.1
AR = arm-none-eabi-ar
AS = arm-none-eabi-as
OBJCOPY = arm-none-eabi-objcopy
OBJDUMP = arm-none-eabi-objdump
SIZE = arm-none-eabi-size
MAKE = make

# project parameters
PROJ_NAME = stm32_gcc
CPU_FAMILY = STM32F1xx
CPU_MODEL_GENERAL = STM32F103xB
CPU_MODEL_SPECIFIC = STM32F103C8

INCLUDEDIRS =
INCLUDEDIRS += Include
INCLUDEDIRS += BSP/StdPeriph_Driver/inc
INCLUDEDIRS += BSP/System
INCLUDEDIRS += BSP/Hardware_Driver
INCLUDEDIRS += Libraries/HAL
# -lc      : link to standard C library ( stdio.h and stdlib.h)
# -lg      : a debugging-enabled libc
# -lm      : link to math C library ( math.h )
# -lnosys  : non-semihosting
#            link to libnosys.a - The libnosys.a is used to satisfy all system call references,
#            although with empty calls. In this configuration, the debug output
#            is forwarded to the semihosting debug channel, vis SYS_WRITEC.
#            The application and redefine all syscall implementation functions, like _write(), 
#            _read(), etc. When using libnosys.a, the startup files are not needed
#            Since 4.8, recommended -specs=nosys.specs instead
# -lrdimon : semihosting
#            link to librdimon.a - implements all system calls via the semihosting API with all 
#            functionality provided by the host. When using librdimon.a, the startup files are
#            required to provide all specific initialisation, and the rdimon.specs
#            must be added to the linker
#            Since 4.8, recommended -specs=rdimon.specs instead

################################################################################
# auto-generated project paths
#SOURCES = main.c gpio.c stm32f1xx_hal_msp.c stm32f1xx_it.c
SYS_SOURCEDIRS =
SYS_SOURCEDIRS += BSP/System
SYS_SOURCEDIRS += BSP/Hardware_Driver

SOURCEDIRS =
SOURCEDIRS += BSP/StdPeriph_Driver/src

LIBRARIES_ROOT_PATH =
LIBRARIES_ROOT_PATH += Libraries/HAL

SOURCES += $(shell find $(SYS_SOURCEDIRS) -name "*.c")
SOURCES += $(shell find Src -name "*.c")
SOURCES += $(shell find $(SOURCEDIRS) -name '*.c')
SOURCES += $(shell find $(LIBRARIES_ROOT_PATH) -name "*.c")
SOURCES += BSP/System/system_stm32f10x.c
SOURCES += BSP/startup/startup_stm32f10x_hd.s
OBJ += $(SOURCES:%.c=%.o)

LDSCRIPT = -Wl,-T,STM32F103C8_FLASH.ld

CFLAGS = -Wall -g -O0 -D $(CPU_MODEL_GENERAL)
CFLAGS += -mlittle-endian -mcpu=cortex-m3 -march=armv7-m -mthumb -DUSE_STDPERIPH_DRIVER -DSTM32F10X_HD -DHSE_VALUE=8000000 -DLSI_VALUE=40000
CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -Wl,--gc-sections -Wl,-Map=$(PROJ_NAME).map 
CFLAGS +=  $(addprefix -I ,$(INCLUDEDIRS))



.PHONY: all

all: $(PROJ_NAME).elf

$(PROJ_NAME).elf: $(OBJ)
	@echo
	$(CC) $(CFLAGS) $^ -o $@ $(LDSCRIPT) --specs=nano.specs --specs=nosys.specs -lc -lm -LD
	$(OBJCOPY) -O ihex $(PROJ_NAME).elf $(PROJ_NAME).hex
	$(OBJCOPY) -O binary $(PROJ_NAME).elf $(PROJ_NAME).bin
	$(OBJDUMP) -St $(PROJ_NAME).elf >$(PROJ_NAME).lst
	$(SIZE) $(PROJ_NAME).elf
	
%.o:%.S
	@echo
	$(CC) $(CFLAGS) -c -MMD -o $@ $< 
	
%.o: %.c
	@echo
	$(CC) $(CFLAGS) -c -MMD -o $@ $<	
		
flash: 
	st-flash write $(PROJ_NAME).bin 0x8000000

clean:
	find $(SYS_SOURCEDIRS) -name "*.d" -exec rm -rf {} \;
	find $(SYS_SOURCEDIRS) -name "*.o" -exec rm -rf {} \;
	
	find $(SOURCEDIRS) -name "*.d" -exec rm -rf {} \;
	find $(SOURCEDIRS) -name "*.o" -exec rm -rf {} \;
	
	find $(LIBRARIES_ROOT_PATH) -name "*.d" -exec rm -rf {} \;
	find $(LIBRARIES_ROOT_PATH) -name "*.o" -exec rm -rf {} \;
	
	find Src -name "*.d" -exec rm -rf {} \;
	find Src -name "*.o" -exec rm -rf {} \;
	
	rm -f libstm32f1.a     || true
	rm -f $(PROJ_NAME).d   || true
	rm -f $(PROJ_NAME).elf || true 
	rm -f $(PROJ_NAME).hex || true
	rm -f $(PROJ_NAME).bin || true
	rm -f $(PROJ_NAME).map || true
	rm -f $(PROJ_NAME).lst || true
	rm -f $(PROJ_NAME).ioc || true 
	rm -f $(PROJ_NAME).elf.launch || true 

